using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using IQIGame.Onigao.GameAOT;
using System;
using System.Collections.Generic;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay
{
    public class UILevelInput : UIEntityWindow
    {
        /// <summary>
        /// 静态配置
        /// </summary>
        private static UICreateConfig _createConfig = new()
        {
            prefabName = nameof(UILevelInput),
            layer = EUILayer.Normal,
        };

        /// <summary>
        /// 创建UI的配置
        /// </summary>
        public override IUICreateConfig createConfig => _createConfig;

        #region Template Generate,don't modify

        protected partial class UIB_UILevelInput
        {
            #region ObjectBinding Generate

            public Joystick joystick { protected set; get; }
            public ExButton btnInteract { protected set; get; }
            public ExImage imgInteract { protected set; get; }
            public GameObject btnPortableItem { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<Joystick>("joystick", out var __tbv0);
                joystick = __tbv0;
                __binding.TryGetVariableValue<ExButton>("btnInteract", out var __tbv1);
                btnInteract = __tbv1;
                __binding.TryGetVariableValue<ExImage>("imgInteract", out var __tbv2);
                imgInteract = __tbv2;
                __binding.TryGetVariableValue<GameObject>("btnPortableItem", out var __tbv3);
                btnPortableItem = __tbv3;
            }



            #endregion ObjectBinding Generate
        }

        #endregion Template Generate,don't modify

        /// <summary>
        /// 可以自定义修改的
        /// </summary>
        protected partial class UIB_UILevelInput
        {
            public virtual void StartBinding(GameObject __go)
            {
                var binding = __go.GetComponent<ObjectBinding>();
                if (binding != null)
                {
                    InitBinding(binding);
                }
            }
        }

        protected UIB_UILevelInput ui { get; set; }

        public event Action onClickInteract;

        private int _curInteractId;

        protected override void BeforeInit()
        {
            ui = new UIB_UILevelInput();
            ui.StartBinding(gameObject);
        }


        private Dictionary<int, Sprite> _inteactImgMapping = new Dictionary<int, Sprite>();

        protected override void OnInit()
        {
            isUpdate = true;
            _portableItemEntrance = InitChildNode<UIN_PortableItemEntrance>(ui.btnPortableItem);
            _portableItemEntrance.Show();
        }

        protected override void OnShow(UIBaseData data)
        {

        }

        protected override void OnHide()
        {

        }

        protected override void AddUGUIListener()
        {
            ui.btnInteract.AddClickEvent(OnBtnInteractClick);
        }

        public void AddJoystickListener(Action<JoystickData> onTouchMove, Action onTouchUp)
        {
            ui.joystick.onTouchMove += onTouchMove;
            ui.joystick.onTouchUp += onTouchUp;
        }

        public void OnBtnInteractClick()
        {
            onClickInteract?.Invoke();
        }

        public void SetInteractState(int interactId)
        {
            if (_curInteractId == interactId)
            {
                return;
            }

            _curInteractId = interactId;
            if (_inteactImgMapping.TryGetValue(interactId, out var sp))
            {
                ui.imgInteract.sprite = sp;
            }
            else
            {
                var cfg = TableCenter.entityInteractType.Get(interactId);
                if (cfg != null)
                {
                    sp = resLoader.LoadABAsset<Sprite>(PathConstant.GetAtlasSpritePath(nameof(UILevelInput), cfg.Icon));
                    _inteactImgMapping[interactId] = sp;
                    ui.imgInteract.sprite = sp;
                }
            }
        }


        protected override void OnDispose()
        {
            onClickInteract = null;
            _inteactImgMapping.Clear();
        }

        private UIN_PortableItemEntrance _portableItemEntrance;
    }
}
